import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './sys/user/user.module';
import env from '../config/env';
import { UserEntity } from './sys/user/entities/user.entity';
import { AuthModule } from './sys/auth/auth.module';
import { AuthEntity } from './sys/auth/entities/auth.entity';
import { APP_GUARD } from '@nestjs/core';
import { JwtAuthGuard } from './sys/auth/jwt-auth.guard';
import { RedisService } from './sys/redis/redis.service';
import { RedisModule } from './sys/redis/redis.module';
import { UploadModule } from './sys/upload/upload.module';
import { LoggerService } from './sys/logger/logger.service';
import { TimerModule } from './sys/timer/timer.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: [env.path],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        return {
          type: 'mysql',
          entities: [UserEntity, AuthEntity],
          host: configService.get('DB_HOST'),
          port: configService.get('DB_PORT'),
          username: configService.get('DB_USER'),
          password: configService.get('DB_PASSWD'),
          database: configService.get('DB_DATABASE'),
          synchronize: false,
          timezone: '+08:00',
        };
      },
    }),
    UserModule,
    AuthModule,
    RedisModule,
    UploadModule,
    TimerModule,
  ],
  controllers: [AppController],
  // 注册为全局守卫
  providers: [AppService, { provide: APP_GUARD, useClass: JwtAuthGuard }, RedisService, LoggerService],
})
export class AppModule {}
